<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/11/19
 * Time: 下午8:12
 */

class ModGood extends Model {

    protected $table = 'goods';

    // 表名
    public function tableName(){
        return $this->table;
    }

    public static function model($className=__CLASS__){
        return parent::model($className);
    }

    public function getDbConnection(){
        return Yii::app()->dbOss;
    }

    public function addmuch($params){
    	$result = $this->getDbConnection()->multiInsert($params, $this->table, FALSE);
    	return $result;
    }
    
    public function select($params){
    	$table = $this->table;
    	$where  = ' WHERE game=:game ';
    	$args   = array(
    			':game'     => $params['g']
    	);
    	 
    	if(!empty($params['g_id'])){
    		$where .= " AND g_id like :g_id ";
    		$args['g_id'] = '%'.$params['g_id'].'%';
    	}
    
    	if(!empty($params['name'])){
    		$where .= " AND name like :name ";
    		$args['name'] = '%'.$params['name'].'%';
    	}

    	if($params['quality'] != ''){
    		$where .= " AND quality=:quality ";
    		$args['quality'] = $params['quality'];
    	}
    	
    	if($params['stage'] != ''){
    		$where .= " AND stage=:stage ";
    		$args['stage'] = $params['stage'];
    	}
    	
    	if($params['star'] != ''){
    		$where .= " AND star=:star ";
    		$args['star'] = $params['star'];
    	}
    	
    	if($params['is_analysis'] != ''){
    		$where .= " AND is_analysis=:is_analysis ";
    		$args['is_analysis'] = $params['is_analysis'];
    	}

    	$fields = "id,g_id,name,quality,stage,star";
    	$sql = "SELECT {$fields} FROM {$table} {$where}";
    	$data = $this->getDbConnection()->query($sql, $args);    	
    	return $data;
    }
    
    public function page($params){
    	$table = $this->table;
    	$where  = ' WHERE game=:game and background_show=:background_show';
    	$args   = array(
    			':background_show' => 1,
    			':game'     => $params['g']
    	);
    	
        if(!empty($params['g_id'])){
            $where .= " AND g_id=:g_id ";
            $args['g_id'] = $params['g_id'];
        }
        
        if(!empty($params['name'])){
        	$where .= " AND name like :name ";
        	$args['name'] = '%'.$params['name'].'%';
        }
    	
    	$c_sql = "select count(1) as cnt from {$table} {$where}";
    	$c_result = $this->getDbConnection()->query($c_sql, $args);
    	$count = isset($c_result[0]['cnt']) ? $c_result[0]['cnt'] : 0;

    	$order  = self::getOrder($params['order']);
    	$limit  = PubFunc::getLimit($params['length'], $params['page']);
    	$fields = "id,g_id,name,quality,stage,star,description,access,item_value,max_overlap";
    	$sql = "SELECT {$fields} FROM {$table} {$where} {$order} {$limit}";
    	
    	$data = $this->getDbConnection()->query($sql, $args);
    	$result = array('count'=>$count,'data'=>$data);
    	return $result;
    }
    
    public static function getOrder($order){
    	if(PubFunc::checkEmpty($order, array(0, 1))) return '';
    	if(!in_array($order[1], array('asc', 'desc'))){
    		$sort = 'asc';
    	}else{
    		$sort = $order[1];
    	}
    	return " ORDER BY {$order[0]} {$sort} ";
    }
    
    public function getGid(){
    	$sql = "SELECT g_id FROM {$this->table}";
    	$result = $this->getDbConnection()->getAll($sql, $args);
    	return $result;
    }
    
    public function getInfo($g_id_array){
    	if(count($g_id_array) == 0){
    		return array();
    	}
    	$g_ids = implode(',', $g_id_array);
    	$g_ids = trim($g_ids,',');
    	$sql = "SELECT g_id,name,quality,stage,star,description FROM {$this->table} WHERE g_id IN ({$g_ids})";
    	$result = $this->getDbConnection()->getAll($sql, $args);
    	return $result;
    }
    
    
} 